package com.sjq.common.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.sjq.common.dao.DeptDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.entity.Dept;
import com.sjq.common.util.PageResults;

@Repository("DeptDao")
public class DeptDaoImpl extends GenericDao<Dept> implements DeptDao {
	
    //新增单位
	@Override
	public void insertDept(Dept dept) {
		StringBuilder sql = new StringBuilder()
				.append(" insert into t_sys_dw_gajg ")
				.append(" (dwbm,dwmc,jc,sjbmdm,djsj,djbgbs,djbgsj) ")
				.append(" values(?,?,?,?,sysdate,'1',sysdate) ");
        Object[] params = new Object[]{
        		dept.getDWBM(),
        		dept.getDWMC(),
        		dept.getJC(),
        		dept.getSJBMDM()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
	}

	//删除
	@Override
	public void deleteDeptById(String deptId) {
		String sql = " update t_sys_dw_gajg set djbgbs='3',djbgsj=sysdate where dwbm=? ";
		this.getJdbcTemplate().update(sql, deptId);
	}
	
    //修改单位
	@Override
	public void updateDept(Dept dept) {
		StringBuilder sql = new StringBuilder()
				.append(" update t_sys_dw_gajg set ")
				.append(" dwmc=?, ")
				.append(" jc=?, ")
				.append(" sjbmdm=?, ")
				.append("djbgbs='2',djbgsj=sysdate where dwbm=?");
		Object[] params = new Object[] {
				dept.getDWMC(),
				dept.getJC(),
        		dept.getSJBMDM(),
        		dept.getDWBM()
		};
		this.getJdbcTemplate().update(sql.toString(), params);
	}
	
	//查询单个部门
	@Override
	public Map<String, Object> selectDeptById(String deptId) {
		StringBuilder sql = new StringBuilder()
				.append(" select dwbm,dwmc,flag,sjbmdm,ssfjdm,jc from t_sys_dw_gajg where dwbm=? ");
		Map<String, Object> map=new HashMap<String,Object>(); 
		try{
			map= this.getJdbcTemplate().queryForMap(sql.toString(),deptId);
		}catch(Exception e){
			map=null;	
		}
			return map;
	}

	//分页查询
	@Override
	public PageResults<?> selectDeptByPage(int pageNo, int pageSize, Dept dept) {
		StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		if (dept.getDWMC() !="-1" && dept.getDWMC() != null&& !(dept.getDWMC().equals(""))) {
			paramSql.append(" and FULL_NAME like ? ");
			params.add("%" + dept.getDWMC().trim() + "%");
		}
		if (dept.getDWBM() !="-1" && dept.getDWBM() != null&& !(dept.getDWBM().equals(""))) {
			paramSql.append(" and ORG_CODE like ? ");
			params.add("%" +dept.getDWBM().trim() + "%");
		}
		PageResults<Map<String, Object>> results = new PageResults<Map<String, Object>>();
		StringBuilder pageSql = new StringBuilder()
				.append(" select * from (select e.*,rownum r from(")
				.append(" select * from T_SYS_JGDW_DWXX where 1=1 ")
				.append( paramSql.toString() +"  order by ORDERCODE desc)e) ")
				.append(" where r between  ")
				.append(((pageNo-1)*pageSize+1))
				
				.append(" and ")
				.append( pageSize*pageNo );
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(pageSql.toString(),params.toArray());
		results.setPageSize(pageSize);
		String sql="select count(1) num from T_SYS_JGDW_DWXX where 1=1  "+paramSql.toString()+" ";
		SqlRowSet sqlRowSet = this.getJdbcTemplate().queryForRowSet(sql,params.toArray());
		int count = 0;
		while(sqlRowSet.next()){
			count = sqlRowSet.getInt("num");
		}
		results.setTotalCount(count);
        results.setPageNo(pageNo);
		results.setResults(list);
		return results;
	}

	@Override
	public String initDeptAllTree() {
		JSONObject json=new JSONObject();
		json.put("text", "宿迁市公安局");
		json.put("dwbm", "321300000000");
		String sql="select * from T_SYS_JGDW_DWXX  order by ordercode ";		
		List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
		return null;
	}

	@Override
	public List<Map<String,String>> initDeptTreeByPid(String type,String pid)  {
		String sql="select org_name,org_code,parent_code,dictionary_name,full_name,org_shortname from t_sys_jgdw_dwxx t where org_type='10' and org_status='2000' and parent_code=? order by ordercode asc";
		Object[] params = new Object[] {pid};
		try {
			return this.getListBySql(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
